<?php

namespace App\Services\Index\Elasticsearch;

use App\Services\Service;
use App\Services\Traits\DatabaseElasticsearch;
use App\Models\Elasticsearch\Settlement\Info as SettlementInfo;
use App\Models\Elasticsearch\Order\Wallet as WalletOrder;
use App\Core\Traits\Numeral;

class ChartService extends Service
{
    use DatabaseElasticsearch, Numeral;

    /**
     * 概要统计【平台收益】
     */
    public function amount($request)
    {
        $params = ['body' => ['size' => 0, 'track_total_hits' => false]];
        // 筛选【类型】
        $this->makeWhere($params, 'player_type', SettlementInfo::PLAYER_TYPE);
        // 筛选【平台】
        $this->makeWhereOrg($request, $params);
        // 筛选【时间】
        $this->makeWhereLastDays($request, $params, 'end_time', 100);
        // 获取时间参数
        $datetime = $this->getParamArray($request, 'datetime');
        // 统计数据【数据列表】
        $params['body']['aggs']['group'] = [
            'date_histogram' => [
                'field' => 'end_time_at',
                'time_zone' => '+08:00',
                'calendar_interval' => '1d',
                'format' => 'yyyy-MM-dd',
                'min_doc_count' => 0,
                'extended_bounds' => [
                    'min' => $datetime[0] * 1000,
                    'max' => $datetime[1] * 1000,
                ],
            ],
            'aggs' => [
                'usd.gain_player' => [
                    'sum' => [
                        'field' => 'usd.gain_player',
                    ]
                ],
            ],
        ];
        // 查询数据
        $results = SettlementInfo::search($params);

        // 制作数据
        return collect(array_map(
            function ($item) {
                return [
                    'timed' => $item['key'] / 1000,
                    'amount' => $this->makeRound(-$item['usd.gain_player']['value']),
                ];
            },
            $results['aggregations']['group']['buckets']
        ));
    }

    /**
     * 统计图【钱包订单】
     */
    public function wallet($request)
    {
        $params = ['body' => ['size' => 0, 'track_total_hits' => false]];
        // 筛选【平台】
        $this->makeWhereOrg($request, $params);
        // 筛选【时间】
        $this->makeWhereLastDays($request, $params, 'created', 100);
        // 筛选【状态】
        $this->makeWhere($params, 'status', WalletOrder::STATUS_SUCCEED);

        // 统计数据
        $this->makeGroupAggs($params, 'sum', ['usd.consume_sum', 'usd.take_back_sum'],
            [
                ['timed' => ['terms' => ['script' => "doc['created'].value - (doc['created'].value + 8 * 3600) % 86400"]]]
            ]
        );
        // 查询数据
        $results = WalletOrder::search($params);

        // 制作数据
        return collect(array_map(
            function ($item) {
                return [
                    'timed' => $item['key']['timed'],
                    'consume_sum' => $this->makeRound($item['usd.consume_sum']['value']),
                    'take_back_sum' => $this->makeRound($item['usd.take_back_sum']['value']),
                ];
            },
            $results['aggregations']['group']['buckets']
        ));
    }
}
